﻿using System;
using System.Linq;
using System.ServiceProcess;
using System.Threading;
using Biz.EF.Gateway.Monitor;
using Biz.Process;
using Hjt212Parser;
using log4net;

namespace Biz
{
    public partial class BizProcessor : ServiceBase
    {
        private readonly ILog _log = LogManager.GetLogger(typeof(BizProcessor));
        int milliSeconds = 1000;
        public BizProcessor()
        {
            InitializeComponent();
        }
        //test
        public void Main()
        {
            OnStart(null);
        }
        /// <summary>
        /// 各路线程
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            //启动各路线程
            //数据解析
            new Thread(ProcessData).Start();
            _log.Info("各路线程启动完成...");
        }
        protected override void OnStop()
        {
            _log.Info("The processor Stoped!");
            Environment.Exit(0);
        }
        private void ProcessData(object state)
        {
            while (true)
            {
                try
                {
                    using (var ctx = new GatewayMonitorContext())
                    {
                        var items = ctx.received_msg.Where(t => t.processed_flag.Equals("NotProcessed")).OrderBy(t => t.datatime)
                            .Take(1000).ToList();
                        _log.Debug("Be processed:" + items.Count);
                        foreach (var item in items)
                        {
                            item.processed_flag = ProcessFlag.ProcessingFinished.ToString();

                            var cn = Parser.GetStringByKey("CN", item.received_string);

                            string node = Parser.GetStringByKey("Node", item.received_string);
                            switch (cn)
                            {
                                case "2011":
                                    if (!string.IsNullOrWhiteSpace(node))
                                    {
                                        UplineFromTgyw.Process2011(item.received_datetime, item.received_string);
                                    }
                                    else
                                    {
                                        UplineFromPksc.Process2011(item.received_string);
                                        UplineFromPkscForMonitor.Process2011(item.received_string);
                                    }
                                    break;
                                case "2051":
                                    UplineFromPksc.Process2051(item.received_string);
                                    UplineFromPkscForMonitor.Process2051(item.received_string);
                                    break;
                                case "2061":
                                    UplineFromPksc.Process2061(item.received_string);
                                    UplineFromPkscForMonitor.Process2061(item.received_string);
                                    break;
                                case "2031":
                                    UplineFromPksc.Process2031(item.received_string);
                                    UplineFromPksc.Process2031ForMonth(item.received_string);
                                    UplineFromPksc.Process2031ForYear(item.received_string);
                                    UplineFromPkscForMonitor.Process2031(item.received_string);
                                    UplineFromPkscForMonitor.Process2031ForMonth(item.received_string);
                                    UplineFromPkscForMonitor.Process2031ForYear(item.received_string);
                                    break;
                                case "0000":
                                    UplineFromPkscForMonitor.SetStatus(item.received_string, false);
                                    break;
                            }
                        }
                        ctx.SaveChanges();
                    }
                    ThreadUtil.Sleep(milliSeconds * 30);
                }
                catch (Exception e)
                {
                    _log.Error(e);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
    }
}
